<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">
          {{ this.isreturn }}
        </div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#313743"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 也可以直接写unique-opened -->
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- i是图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- span是文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- i是图标 -->
                <i class="el-icon-menu"></i>
                <!-- span是文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容区域 -->
      <el-main>
        <!-- 放置路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
      //是否折叠 false为不折叠
      isCollapse: false,
      //收回/展开按钮的默认值
      isreturn: '<' + '收起',
      //被激活的链接地址
      activePath: '',
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginout() {
      //退出功能实现原理
      //基于 token 的方式实现退出比较简单，只需要销毁本地的 token即可。
      //这样，后续的请求就不会携带token，必须重新登录生成一个新的 token
      //之后才可以访问页面。
      //清空token
      window.sessionStorage.clear()
      //跳转到登录页
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    toggleCollapse() {
      //点击按钮切换菜单的折叠与展开
      this.isCollapse = !this.isCollapse
      if (this.isCollapse) {
        this.isreturn = '展开' + '>'
      } else {
        this.isreturn = '<' + '收起'
      }
    },
    //保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
  },
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #363d40;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      padding-left: 20px;
    }
  }
}
.el-aside {
  background-color: #313743;
  // 去掉侧边栏的border-right的1px的右边框
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #e9edf0;
}
.home-container {
  height: 100%;
}
.iconfont {
  padding-right: 13px;
}
.toggle-button {
  background-color: #475163;
  font-size: 15px;
  line-height: 56px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em; //将|||分隔开
  cursor: pointer; //移动到该区域显示小手
}
</style>